import { message } from 'antd';
import axios, { AxiosRequestConfig, AxiosResponse, AxiosError } from 'axios';
const setupAxiosInterceptors = () => {
    const onRequestSuccess = (config: AxiosRequestConfig) => config;
    const onResponseSuccess = (response: AxiosResponse) => {
        try {
            return Promise.resolve(response.data.data);
        } catch (e) {
            message.error('出错了');
        }
        return Promise.resolve(null);
    };
    const onResponseError = (err: AxiosError | any) => {
        const { response } = err;
        // 这里根据出错类型分别处理
        // 4开头的为登录问题，return code
        switch (response.status) {
        case 401:
        case 403:
        case 404:
        case 405:
            return Promise.reject(response.status);
        }
        if (response.data && response.data.message) {
            message.error('出错了');
        }
        return Promise.resolve(null);
    };
    axios.interceptors.request.use(onRequestSuccess);
    axios.interceptors.response.use(onResponseSuccess, onResponseError);
};

export default setupAxiosInterceptors;
